package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePhoneNumber.persenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePhoneNumber.bean.PhoneNumberBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePhoneNumber.model.FirstChangeChangePhoneNumberModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePhoneNumber.model.ISFirstChangeChangePhoneNumberModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePhoneNumber.view.ISFirstChangeChangePhoneNumberView;
import com.example.anew.journey.mynewjourney.logic.getVersion.bean.GetVersionBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司16
 * Created by 20433 on 2018/3/16
 */

public class FirstChangeChangePhoneNumberPersenter {
    private ISFirstChangeChangePhoneNumberView mISFirstChangeChangePhoneNumberView;
    private ISFirstChangeChangePhoneNumberModel mISFirstChangeChangePhoneNumberModel ;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.arg1 == 0) {
                flag = true;
                mISFirstChangeChangePhoneNumberView.setVersionCode("获取验证码");
            } else {
                flag = false;
                mISFirstChangeChangePhoneNumberView.setVersionCode(msg.arg1+" S" );
            }
        }
    };
    private boolean flag = true;
    public FirstChangeChangePhoneNumberPersenter(ISFirstChangeChangePhoneNumberView iSFirstChangeChangePhoneNumberView) {
        this.mISFirstChangeChangePhoneNumberView = iSFirstChangeChangePhoneNumberView;
        mISFirstChangeChangePhoneNumberModel = new FirstChangeChangePhoneNumberModel();
    }

    //点击完成
    public void onClickbBtComplete(Context mContext) {
        TakeDateBean bean  = mISFirstChangeChangePhoneNumberModel.TakeDate(mContext);
        if(bean == null  ) return;
        String changePhoneNumber = mISFirstChangeChangePhoneNumberView.getInputPhoneNumber();
        String versioncode = mISFirstChangeChangePhoneNumberView.getInputVersionCode();
        if (TextUtils.isEmpty(changePhoneNumber))
            mISFirstChangeChangePhoneNumberView.showToastInformation("请输入更换的手机号");
        if (TextUtils.isEmpty(versioncode))
            mISFirstChangeChangePhoneNumberView.showToastInformation("请输入验证码");
        submitChangePhoneNumber(changePhoneNumber,versioncode,bean.getUser_id(),mContext);

    }

    //点击获取验证码
    public void onClickeGetVersionCode() {
        String changePhoneNumber = mISFirstChangeChangePhoneNumberView.getInputPhoneNumber();
        if (TextUtils.isEmpty(changePhoneNumber))
            mISFirstChangeChangePhoneNumberView.showToastInformation("请输入更换的手机号");
        if(flag) changTime();

        getVersionCode(changePhoneNumber);
    }
    // 提交 更换的手机号 验证码
    private void submitChangePhoneNumber(final String changePhoneNumber, String versioncode, String user_id, final Context mContext) {
      //
      //  mISFirstChangeChangePhoneNumberView.showToastInformation("提交成功");
        String url = RBConstants.SERVER_URL+RBConstants.CHANGE_PHONE_NUMBER;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id",user_id)
                .addParams("tel", changePhoneNumber)
                .addParams("code", versioncode)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                        //NLogger.e("TAG","============NetworkResponseDate================="+NetworkResponseDate);
                      //  final NicknameBean nicknameBean =   gson.fromJson(NetworkResponseDate, NicknameBean.class);
                        final PhoneNumberBean phoneNumberBean =  gson.fromJson(NetworkResponseDate, PhoneNumberBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                            if(phoneNumberBean.errorCode == 200){
                                mISFirstChangeChangePhoneNumberModel.savePhoneNumber(changePhoneNumber,mContext);
                                mISFirstChangeChangePhoneNumberView.showToastInformation("手机号更换成功");
                                mISFirstChangeChangePhoneNumberView.onClickeComplete();
                            }else{
                                mISFirstChangeChangePhoneNumberView.showToastInformation("手机号更换失败");
                               // mISFirstChangeChangePhoneNumberView.onClickeComplete();
                            }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                     //   mISFirstChangeChangePhoneNumberView.showToastInformation("请查看网络");
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private void  changTime(){
        flag = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 59; i >= 0 ; i--) {
                    try {
                        Message msg = mHandler.obtainMessage();
                        msg.arg1 = i;
                        mHandler.sendMessage(msg);
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                }
            }
        }).start();
    }

    public void getVersionCode(String changePhoneNumber) {
        String url = RBConstants.SERVER_URL+RBConstants.REGISTERED_GET_VERSION_CODE;
        OkHttpUtils
                .get()
                .url(url)
                .addParams("tel", changePhoneNumber)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson =   new Gson();
                       // NLogger.e("TAG","============getVersionCode================="+NetworkResponseDate);
                        final GetVersionBean getVersion =  gson.fromJson(NetworkResponseDate,GetVersionBean.class);
                        // TODO 移植
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(getVersion.errorCode == 200) mISFirstChangeChangePhoneNumberView.showToastInformation("验证码发送成功");
                                if(getVersion.errorCode == 400)  mISFirstChangeChangePhoneNumberView.showToastInformation("不能访问");
                                if(getVersion.errorCode == 500)   mISFirstChangeChangePhoneNumberView.showToastInformation("未访问到服务");
                            }
                        });


                        return null;
                    }
                    @Override
                    public void onError(Call call, Exception e, int id) {
                       // mISFirstChangeChangePhoneNumberView.showToastInformation("请查看网络");
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
